package hejie.admin.service.impl;

import java.util.List;

import hejie.admin.dao.LoanTypeDao;
import hejie.admin.service.LoanTypeService;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import publics.model.LoanType;
import publics.util.Page;


@Component("loanTypeService")
public class LoanTypeServiceImpl implements LoanTypeService{
	
	LoanTypeDao loanTypeDao;
	
	public LoanTypeDao getLoanTypeDao() {
		return loanTypeDao;
	}	
	@Resource(name="loanTypeDao")
	public void setLoanTypeDao(LoanTypeDao loanTypeDao) {
		this.loanTypeDao = loanTypeDao;
		System.out.println("loanTypeDaoImpl inject in LoanTypeServiceImpl");
	}

	public Object saveLoanType(LoanType ltype) {
		List<LoanType> ltypeName = loanTypeDao.findByName(ltype.getName());	
		List<LoanType> ltypeNum = loanTypeDao.findByNum(ltype.getNum());	
		if(ltypeName.size()==0&&ltypeNum.size()==0){
			return loanTypeDao.save(ltype);		
		}
		else 			
		   return null;
	}

	public Page findByPage(Page page) {
		String sql = "from LoanType l";		 
		if(page.getConditions().size() > 0){//这里为模糊查询的 sql 语句拼接
			sql = sql + " where ";  
			for(int i = 0; i < page.getConditions().size(); ){
				sql = sql + "( l.name like '%"+page.getConditions().get(i)+"%'"
				+" or l.num like '%"+page.getConditions().get(i)+"%' ) ";
				//每次上面两个属性只有匹配到的才能取到值，不然为%
				i++;
				if(i < page.getConditions().size()){// 如果条件不止一个 进行连续拼接
					sql = sql + " and ";
				}
			}
		}
		System.out.println("sql ="+sql);
		
		int pageSize = page.getLimit();
		int offset = page.getStart();//-1)*pageSize;
		page.setRoot(loanTypeDao.findByPage(sql,offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(loanTypeDao.findByCount(sql));   //把数据总数放入totalProperty中
		return page;
	}

	public boolean updateLoanType(LoanType ltype) {
		//List<LoanType> ltypeName = loanTypeDao.findByName(ltype.getName());	
		//List<LoanType> ltypeNum = loanTypeDao.findByNum(ltype.getNum());	
		//System.out.println("service里 ltypeName="+ltypeName.size());
		//System.out.println("service里 ltypeNum="+ltypeNum.size());
		//if(ltypeName.isEmpty()||ltypeNum.isEmpty()){
			return loanTypeDao.update(ltype);
								
		//}
		//else
			//System.out.println("service里 loanTypeDao.update(ltype)="+loanTypeDao.update(ltype));
		//return false;	
	}
	

	public boolean deleteLoanType(Integer id) {
		System.out.println("service里 loanTypeId="+id);
		return loanTypeDao.deleteById(id);
	} 	
}
